import { createRouter, createWebHistory } from 'vue-router'
import DefaultLayout from '@/layouts/DefaultLayout.vue'

const routes = [
  {
    path: '/',
    component: DefaultLayout,
    children: [
      {
        path: '',
        name: 'Home',
        component: () => import('@/views/Home.vue'),
      },
      {
        path: '/about',
        name: 'About',
        component: () => import('@/views/About.vue')
      },
      {
        path: '/article/:id',
        name: 'ArticleDetail',
        component: () => import('@/views/ArticleDetail.vue'),
        meta: {
          hideSidebar: true // 隐藏默认侧边栏
        }
      },
      {
        path: '/category/:id',
        name: 'Category',
        component: () => import('@/components/blog/ArticleList.vue'),
        props: true // 添加props: true以接收路由参数
      },
      {
        path: '/tag/:id',
        name: 'Tag',
        component: () => import('@/components/blog/ArticleList.vue'),
        props: true // 添加props: true以接收路由参数
      },
      {
        path: '/message',
        name: 'Guestbook',
        component: () => import('@/views/Guestbook.vue'),
        meta: {
          hideSidebar: true // 添加隐藏侧边栏标记
        }
      },
      {
        path: '/write',
        name: 'WriteArticle',
        component: () => import('@/views/Article.vue'),
        meta: {
          hideSidebar: true // 添加隐藏侧边栏标记
        }
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes,
  scrollBehavior(to, from, savedPosition) {
    // 从文章详情返回时恢复滚动位置
    if (from.name === 'ArticleDetail' && savedPosition) {
      return savedPosition
    }
    // 进入文章详情页时滚动到顶部
    if (to.name === 'ArticleDetail') {
      return { top: 0 }
    }
    // 其他情况保持当前位置
    return savedPosition || { top: 0 }
  }
})
export default router